package ar.edu.itba.it.pdc.protocol;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import ar.edu.itba.it.pdc.protocol.exception.InvalidPOP3FormatException;


public class ValidPOP3Response extends POP3Response {
	
	public static enum Status {
		OK("+OK"),
		ERR("-ERR");
		
		String caption;
		
		private Status(String caption){
			this.caption = caption;
		}
		
		public String print() {
			return this.caption;
		}
		
		public static Status get(String name) {
			String str = name.substring(1);
			return Status.valueOf(str);
        }
	}
	
	private Status status;
	private String arguments;
	
	public ValidPOP3Response(File file) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		
		String line = reader.readLine();
		
		if(line == null)
			throw new InvalidPOP3FormatException("Invalid POP3 Response.");
		
		try {
			String[] parts = line.split(" ", 2);
			
			this.status = Status.get(parts[0]);
			
			if(parts.length > 1)
				this.arguments = parts[1];
			
		} catch(Exception e) {
			throw new InvalidPOP3FormatException("Invalid POP3 Response.");
		}
	}
	
	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}
	
	public boolean is(Status status){
		return this.status.equals(status);
	}

	public String getArguments() {
		return arguments;
	}

	public void setArguments(String arguments) {
		this.arguments = arguments;
	}
	
	public boolean hasArguments() {
		return arguments != null;
	}
	
	public boolean isValid(){
		return true;
	}
}
